package review.system.action;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import review.config.Constants;
import review.entity.Roles;
import review.system.service.RoleService;

import java.util.List;

@Controller
@RequestMapping("/syatem")
@Api(tags = {"角色操作接口"})
public class RoleAction {

  @Autowired
  private RoleService roleService;

  @ApiOperation(value = "添加角色", notes = "", httpMethod = "POST")
  @RequestMapping(value="/add_roles",produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object add(Roles roles) {
    JSONObject obj = new JSONObject();
    try {
      roleService.addRoles(roles);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, roles);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "删除角色", notes = "", httpMethod = "POST")
  @RequestMapping(value="/delete_roles",produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object delete(Roles roles) {
    JSONObject obj = new JSONObject();
    try {
      roleService.deleteRoles(roles);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, roles);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "修改角色", notes = "", httpMethod = "POST")
  @RequestMapping(value="/update_roles",produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object update(Roles roles) {
    JSONObject obj = new JSONObject();
    try {
      roleService.updateRoles(roles);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, roles);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "查询所有角色", notes = "", httpMethod = "GET")
  @RequestMapping(value="/query_roles",produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object query(Roles roles) {
    JSONObject obj = new JSONObject();
    try {
      List<Roles> list = roleService.findAllRoles();
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, list);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }
}
